<?php 
class Verify extends MY_Controller {
    
    function __construct() {
        parent::__construct();
        
		$this->load->library('form_validation');
		$this->load->model('user_model', '', TRUE);
        $this->load->model('society_model', '', TRUE);
        $this->load->model('association_model', '', TRUE);
        $this->load->model('teacher_model', '', TRUE);
        
        $this->load->library('col_pro_data');
    }
    
	/**
	 * 社团审核首页
	 */
 	function index($param = "1-0") {
 		
		$url_array = explode("-", $param);
		$data['url_array'] = $url_array;
    	$data['status_array'] = $this->_get_status_array($url_array[1]);
        $data['category_array'] = $this->_get_category_array($url_array[0]);
		
		$condition = $this->_get_condition($url_array);
		//$data['society_by_status_and_category'] = $this->society_model->search_by_condition($condition);
		$data['society_by_status_and_category_count'] = $this->society_model->search_by_condition($condition, "", TRUE);
		
		//---------------------------------- 分页开始 ------------------------------------//
        $this->load->library('pagination');
        $config['base_url'] = BACKEND_ASSOCIATION_VERIFY_PATH . '/' . $param;
        $config['total_rows'] = $data['society_by_status_and_category_count'];
        $config['uri_segment'] = 6;
        $config['per_page'] = PER_PAGE;
        //$config['num_links'] = 5;  //页码链接偏移量
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = "class='anchor_button' style='font-size: 80%;'"; 
        
        $this->pagination->initialize($config);

		$condition['per_page'] = PER_PAGE;
		$condition['offset'] = $this->uri->segment(6);
		
		$data['society_by_status_and_category'] = $this->society_model->search_by_condition($condition);
		//---------------------------------- 分页结束 ------------------------------------//
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
		$this->load->view('backend/association/verify_society', $data);
 	}
 	
	function _get_condition($url_array) {
        
    	if ($url_array[0] != 0) {
			if ($url_array[0] == 1) {
				$condition['confirm_FLAG'] = SOCIETY_TO_VERIFY;
			} elseif ($url_array[0] == 2){
				$condition['confirm_FLAG'] = SOCIETY_TRIAL_PASS;
			} elseif ($url_array[0] == 3) {
				$condition['confirm_FLAG'] = SOCIETY_TRIAL_FAIL;
			}
		}
		if ($url_array[1] != 0) {
			$condition['category'] = "$url_array[1]";
		}
		if (!empty($condition)) {
			return $condition;
		}
    }
    
	function _get_status_array($category) {
    	$status_array = $this->common_data->_get_society_status_for_association();
    	foreach ($status_array as $k => $v) {
    		$status_array[$k] = array('href'=>"{$k}-{$category}", 'name'=>"{$v}", 'id'=>"{$k}");
    	}
        return $status_array;
    }
    
	function _get_category_array($confirm_FLAG_id) {
    	$category_array = $this->common_data->_get_society_category_name_by_cid();
    	foreach ($category_array as $k => $v) {
    		$category_array[$k] = array('href'=>"{$confirm_FLAG_id}-{$k}", 'name'=>"{$v}", 'id'=>"{$k}");
    	}
        return $category_array;
    }
    
	function ajax_verify_pass() {
		$this->_update_society_status_and_signin_log($this->input->post('society_id'), $this->_get_association_name(), ASSOCIATION_STGLB_NAME, SOCIETY_TRIAL_PASS, $this->input->post('remark'));
    }
    
	function ajax_verify_failed() {
		$this->_update_society_status_and_signin_log($this->input->post('society_id'), $this->_get_association_name(), ASSOCIATION_STGLB_NAME, SOCIETY_TRIAL_FAIL, $this->input->post('remark'));
    }
    
    function ajax_get_so_info() {
    	$data = $this->_get_society_info($this->input->post("society_id"));
    	
    	//echo "<pre>";
    	//print_r($data);
    	//return ;
    	$this->output->set_output(json_encode($data));
    }
    
	function ajax_get_so_log() {
    	$temp = $this->society_model->_get_by_foreign_id($this->input->post("society_id"));
    	$data['s_name'] = $temp['s_name'];
    	$data['signin_log'] = $this->_get_society_sigin_log($temp['signin_log']);
    	
    	$this->output->set_output(json_encode($data));
    	//echo "<pre>";
    	//print_r($data);
    	//return ;
    }
    
    function ajax_oneclick_verify() {
    	
    	//第一步：将所有初审完成的社团设置为复审完成
    	$first_society_id_array = $this->society_model->_get_by_specified_key('confirm_FLAG', SOCIETY_TRIAL_PASS, 'society_id');
    	//echo "<pre>";
    	//print_r($first_society_id_array);
    	//return ;
    	if (!empty($first_society_id_array)) {
	    	foreach ($first_society_id_array as $society_id) {
	    		$this->_update_society_status_and_signin_log($society_id, $this->_get_association_name(), ASSOCIATION_STGLB_NAME, SOCIETY_REVIEW_PASS, '', 'oneclick');
	    	}
    	}
    	//return ;
    	
    	//第二步：将所有复审完成的社团设置为终审完成
	    $second_society_id_array = $this->society_model->_get_by_specified_key('confirm_FLAG', SOCIETY_REVIEW_PASS, 'society_id');
	    //echo "<pre>";
    	//print_r($second_society_id_array);
    	//return ;
    	if (!empty($second_society_id_array)) {
	    	foreach ($second_society_id_array as $society_id) {
	    		$this->_update_society_status_and_signin_log($society_id, $this->_get_association_name(), ASSOCIATION_STGLB_NAME, SOCIETY_FINAL_PASS, '', 'oneclick');
	    	}
    	} else {
    		$this->output->set_output(json_encode("failed"));
    		return ;
    	}
    	
    	$this->output->set_output(json_encode("success"));
    }

}